package com.atguigu.gmall.cart.exception.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Slf4j
@Component
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    private static final String EXCEPTION_KEY = "cart:exception";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        // 记录到数据库 记录到日志
        BoundSetOperations<String, String> setOps = this.redisTemplate.boundSetOps(EXCEPTION_KEY);
        setOps.add(params[0].toString());
        log.error("异步任务出现了异常。方法：{}，参数：{}，异常信息：{}", method.getName(), Arrays.asList(params), ex.getMessage());
    }
}
